package com.yinhe.module.yzx.lp;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;

import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;


import com.yinhe.system.domain.YhContent;


import com.yinhe.system.msg.mssage.domain.YhMsgInner;
import com.yinhe.system.msg.ws.service.WebSocketService;
import com.yinhe.system.service.ISysUserService;
import com.yinhe.system.service.IYhContentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/yzx/lp/legalpolicy")
public class YzxLegalpolicyController extends BaseController {
    @Autowired
    private IYhContentService yhContentService;
    @Autowired
    public WebSocketService webSocketService;
    @Autowired
    public ISysUserService iSysUserService;

    /**
     * 查询内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:lp:legalpolicy:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhContent yhContent) {
        startPage();
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        return getDataTable(list);
    }

    /**
     * 导出内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:lp:legalpolicy:export')")
    @Log(title = "内容公告发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContent yhContent) {
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        ExcelUtil<YhContent> util = new ExcelUtil<YhContent>(YhContent.class);
        util.hideColumn("noticeType", "pictureUrl", "sourcesUrl", "participants", "viewsTimes", "haveExperience",
                "experienceContent", "fileNum", "userId", "deptId", "noticeId",
                "note2", "note1", "updateTime",
                "note3", "note4", "note5", "note6", "note7", "note8", "note9", "note10"
        );
        util.exportExcel(response, list, "法规制度");
    }

    /**
     * 获取内容公告发布详细信息
     */
    /*    @PreAuthorize("@ss.hasPermi('yzx:lp:legalpolicy:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(yhContentService.selectYhContentById(id));
    }

    /**
     * 新增内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('yzx:lp:legalpolicy:add')")
    @Log(title = "内容公告发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhContent yhContent) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhContent.setUserId(sysUser.getUserId());
        yhContent.setDeptId(sysUser.getDeptId());

        String Note1 = sysUser.getNickName();
        String Note2 = "/zonghe/yzxfgzdliebiao";
        if (yhContent.getNote3() != null) {
            YhMsgInner msgInner = new YhMsgInner();
            msgInner.setMsgTitle("新增一条法规制度");
            msgInner.setMsgContent(yhContent.getNoticeTitle());
            msgInner.setSendUserName(sysUser.getUserName());
            msgInner.setNote1(Note1);
            msgInner.setNote2(Note2);
            msgInner.setContentType("");
            String[] split = yhContent.getNote3().split(",");
            List<String> receiveNames = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                receiveNames.add(split[i]);
            }
            webSocketService.PushMsg(msgInner, receiveNames);
        }
        if (yhContent.getNote6() != null) {
            YhMsgInner msgInner = new YhMsgInner();
            msgInner.setMsgTitle("新增一条法规制度");
            msgInner.setMsgContent(yhContent.getNoticeTitle());
            msgInner.setSendUserName(sysUser.getUserName());
            msgInner.setNote1(Note1);
            msgInner.setNote2(Note2);
            msgInner.setContentType("");
            String[] split = yhContent.getNote6().split(",");
            List<String> receiveNames = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                Long q = Long.valueOf(split[i]);
                List<SysUser> result = yhContentService.selectUserByDeptId1(q);
                for (int j = 0; j < result.size(); j++) {
                    receiveNames.add(result.get(j).getUserName());
                }
            }
            webSocketService.PushMsg(msgInner, receiveNames);
        }
        return toAjax(yhContentService.insertYhContent(yhContent));
    }

    /**
     * 修改内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('yzx:lp:legalpolicy:edit')")
    @Log(title = "内容公告发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhContent yhContent) {
        return toAjax(yhContentService.updateYhContent(yhContent));
    }

    /**
     * 删除内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('yzx:lp:legalpolicy:remove')")
    @Log(title = "内容公告发布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhContentService.deleteYhContentByIds(ids));
    }
}
